﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.IO;
using System.Net;
using NetOpen_System.Component.QQWry;

namespace NetOpen_System.Component
{
    public sealed class WebsiteFilterHttpModule : IHttpModule
    {
        #region IHttpModule 成员

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }


        #endregion


        void context_BeginRequest(object sender, EventArgs e)
        {
            try
            {
                //HttpApplication objApp = (HttpApplication)sender;

                if (HttpContext.Current.Request.IsLocal)//忽略本地计算机请求
                    return;

                string ip = HttpContext.Current.Request.UserHostAddress;
               

                QQWryLocator qqWry = new QQWryLocator(HttpContext.Current.Server.MapPath(@"~\IpData\qqwry.dat"));

                IPLocation ipaddress = qqWry.Query(ip);  //查询一个IP地址

                UrlMatchEngine pu = WebsiteFilterConfiguration.GetConfig().PickedUrls;


                if (string.IsNullOrEmpty(pu.CitySiteList) == false)
                {
                    if (pu.CitySiteList.Contains(ipaddress.Country) == false)
                    {

                        if (!WebsiteFilterConfiguration.GetConfig().IpChecks.GetIpIn(ip))
                        {   //若在IP列表中找不到访客ip

                            //string rawUrl = HttpContext.Current.Request.RawUrl;
                            //UrlMatchEngine pu = WebsiteFilterConfiguration.GetConfig().PickedUrls;

                            ////列表包含当前url且列表为黑名单、列表不包含当前url且列表不为黑名单  时需转向
                            ////换而言之，“配备结果”与“是否黑名单”取值一致时需转向
                            //if (pu.IsMatch(rawUrl) == pu.IsBlacklist)
                            //{   //非公开url自动重定向
                            //    HttpContext.Current.Response.Redirect(pu.ErrorPage);
                            //}
                            HttpContext.Current.Response.Redirect(pu.ErrorPage, true);
                            //HttpContext.Current.Server.Transfer(pu.ErrorPage);

                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (!WebsiteFilterConfiguration.GetConfig().IpChecks.GetIpIn(ip))
                    {   //若在IP列表中找不到访客ip

                        //string rawUrl = HttpContext.Current.Request.RawUrl;
                        //UrlMatchEngine pu = WebsiteFilterConfiguration.GetConfig().PickedUrls;

                        ////列表包含当前url且列表为黑名单、列表不包含当前url且列表不为黑名单  时需转向
                        ////换而言之，“配备结果”与“是否黑名单”取值一致时需转向
                        //if (pu.IsMatch(rawUrl) == pu.IsBlacklist)
                        //{   //非公开url自动重定向
                        //    HttpContext.Current.Response.Redirect(pu.ErrorPage);
                        //}
                        HttpContext.Current.Response.Redirect(pu.ErrorPage, true);
                        //HttpContext.Current.Server.Transfer(pu.ErrorPage);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch
            {

            }
        }
    }
}
